#!/usr/bin/env python

# Copyright (c) Prophesee S.A.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

import pytest
from metavision_utils import shell_tools, pytest_tools


def pytestcase_test_metavision_software_info_show_help():
    """
    Checks output of metavision_software_info when displaying help message
    """

    cmd = "./metavision_software_info --help"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check that the options showed in the output
    assert "Options:" in output, "******\nMissing options display in output :{}\n******".format(output)


def pytestcase_test_metavision_software_commit():
    """
    Checks output of metavision_software_info when retrieving commit
    """

    # Get expected output
    commit_command = "@GIT_COMMAND_GET_COMMIT_ID_QUOTES_ESCAPED@"
    expected_output, error, err_code = shell_tools.execute_cmd(commit_command)
    assert err_code == 0, "******\nError while executing cmd '{}':{}\n{}\n******".format(
        commit_command, expected_output, error)

    # Run app
    cmd = "./metavision_software_info --commit"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check that we get the expected output
    assert output.rstrip() == expected_output.rstrip()


def pytestcase_test_metavision_software_date():
    """
    Checks output of metavision_software_info when retrieving date
    """

    # Get expected output
    commit_command = "@GIT_COMMAND_GET_COMMIT_DATE_QUOTES_ESCAPED@"
    expected_output, error, err_code = shell_tools.execute_cmd(commit_command)
    assert err_code == 0, "******\nError while executing cmd '{}':{}\n{}\n******".format(
        commit_command, expected_output, error)

    # Run app
    cmd = "./metavision_software_info --date"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check that we get the expected output
    assert output.rstrip() == expected_output.rstrip()


def pytestcase_test_metavision_software_version():
    """
    Checks output of metavision_software_info when retrieving version
    """

    # Get expected output
    expected_output = "@PROJECT_VERSION_FULL@"

    # Run app
    cmd = "./metavision_software_info --version"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check that we get the expected output
    assert output.rstrip() == expected_output.rstrip()
